package org.jhub1.system.xmpp.proxy;

import org.joda.time.DateTime;

public class ProcessorSession {

	private static ProcessorSession instance = null;
	private static DateTime expires = null;
	private static boolean mirrorIQ = false;
	private static String sessionOwner = null;
	private static int sessionMinutes;
	
	protected ProcessorSession(int mins) {
		sessionMinutes = mins;
	}

	protected ProcessorSession() {
		sessionMinutes = 15;
	}
	
	public static ProcessorSession getInstance(int hours) {
		if (instance == null) {
			instance = new ProcessorSession(hours);
		}
		return instance;
	}

	public static ProcessorSession getInstance() {
		if (instance == null) {
			instance = new ProcessorSession();
		}
		return instance;
	}
	
	public static void startSession(String jid) {
		expires = new DateTime().plusMinutes(sessionMinutes);
		sessionOwner = jid;
	}

	public static void prolongSession() {
		expires = new DateTime().plusMinutes(sessionMinutes);
	}
	
	public static void killSession() {
		expires = null;
		sessionOwner = null;
		mirrorIQ = false;
	}

	public static void setTimeout(int mins) {
		sessionMinutes = mins;
	}
	
	public static void setMirrorIQ(boolean mirror) {
		mirrorIQ = mirror;
	}
	
	public static int getSessionTime() {
		return sessionMinutes;
	}
	
	public static boolean isMirrorIQActive() {
		return mirrorIQ;
	}
	
	public static boolean isActive() {
		if (expires != null && sessionOwner != null) {
				return expires.isAfterNow();
		}
		return false;
	}
	
	public static boolean isActiveForUser(String jid) {
		if (expires != null && sessionOwner != null) {
			if (sessionOwner.equalsIgnoreCase(jid)) {
				return expires.isAfterNow();
			}
		}
		return false;
	}
}
